<?php

namespace App\Http\Controllers\ChatGroup\Merchant;

use App\Http\Controllers\BaseController;
use App\Http\Middleware\OpenShop\MerchantAppAuth;
use App\Service\ChatGroup\ChatGroupMemberService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class GroupMemberController extends BaseController
{
    /**
     * 获得群组成员
     * @param Request $request
     * @param int $groupId
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function list(Request $request, int $groupId): JsonResponse
    {
        $shopId = $request->header('shopId');

        // 当前用户
        $userId = MerchantAppAuth::$userId;

        return $this->success(ChatGroupMemberService::list($shopId, $groupId, $userId));
    }


    /**
     * 修改群成员
     * @param Request $request
     * @param int $groupId
     * @return JsonResponse
     * @throws ValidationException
     */
    public function updateExist(Request $request, int $groupId): JsonResponse
    {
        $this->validate($request, [
            'userId'  => 'required|int',
            'isExist' => 'required|bool'
        ]);

        $shopId  = $request->header('shopId');
        $userId  = $request->post('userId');
        $isExist = $request->post('isExist');

        // 操作人
        //$operator = MerchantAppAuth::$userId;

        ChatGroupMemberService::updateExist($shopId, $userId, $isExist, $groupId);

        return $this->success();
    }
}
